<?php

/*
 * This class is used for as Model for Admin table 
 */

class Model_Admin extends Model_Base {

    protected $_name = 'tbl_admin';
    protected $_primaryKey = 'adm_id';

    /* stored value in session when admin login */

    public function getAdminAuthData($dataArray = Array()) {
        $select = $this->select()
                ->from(array('lgn' => 'tbl_login'))
                ->setIntegrityCheck(false)
                ->join(array('adm' => 'tbl_admin'), "lgn.lgn_id = adm.lgn_id")
                ->where("adm.lgn_id = '" . $dataArray['lgn_id'] . "'")
                ->where("lgn.lgn_id = '" . $dataArray['lgn_id'] . "'");
        $data = parent::fetchRow($select);

        if ($data->adm_issuperadmin == 1) {
            $roletype = 'SuperAdmin';
        } else {
            $roletype = 'Admin';
        }

        $languageModel = new Model_Language();
        $languagedefaultArray = $languageModel->fetchType('lang_id = ' . $data->lang_id);

        $object = new stdClass();
        $object->usr_id = $data->adm_id;
        $object->lgn_id = $data->lgn_id;
        $object->lgn_email = $data->lgn_email;
        $object->lgn_type = $data->lgn_type;
        $object->name = $data->adm_firstname . " " . $data->adm_lastname;
        $object->role = "ADMIN";
        $object->roletype = $roletype;
        $object->lgn_active = $data->lgn_active;
        $object->company_id = '';
        $object->usr_lang_id = $data->lang_id;
        $object->usr_lang_code = strtolower($languagedefaultArray[0]['lang_code']);
        return $object;
    }

    //get country data
    public function getcountryArray() {
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    public function getlanguageArray() {
        $languageModel = new Model_Language();
        $languageArray = $languageModel->language_fetchall();
        $where = 'lang_isdefault=1';
        $languagedefaultArray = $languageModel->fetchType($where);
        $finalArray[$languagedefaultArray[0]['lang_id']] = $languagedefaultArray[0]['lang_name'] . " [" . strtolower($languagedefaultArray[0]['lang_code']) . "]";
        foreach ($languageArray as $com) {
            $finalArray[$com['lang_id']] = $com['lang_name'] . " [" . strtolower($com['lang_code']) . "]";
        }
        return $finalArray;
    }

    public function getStatusArray() {
        return array(
            '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        //array('child_quality' => 'child_name')        
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('adm' => 'tbl_admin'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = adm.updated_lgn_id")
                ->join(array('lgn_adm' => 'tbl_login'), "lgn_adm.lgn_id = adm.lgn_id", array('lgn_adm_email' => 'lgn_email'))
                ->join(array('country' => 'tbl_country'), "country.country_id = adm.adm_country")
                ->where('adm_id = ?', $id)
                ->where('lgn.lgn_active != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('adm' => 'tbl_admin'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = adm.lgn_id")
                ->where('adm_id = ?', $id)
                ->where('lgn_active != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get admins
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchAdmin($where = "", $limit = "", $offset = "0", $order = 'adm_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('adm' => 'tbl_admin'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = adm.lgn_id")
                ->where('lgn_active != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getAdminCount($where) {
        $select = $this->select()
                ->from(array('adm' => 'tbl_admin'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = adm.lgn_id")
                ->where('lgn_active != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    //get data of admin if login type ADMIN

    public function getadminId($lgn_id) {
        $select = $this->select()
                ->from(array('adm' => 'tbl_admin'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = adm.lgn_id")
                ->where('adm.lgn_id = ?', $lgn_id)
                ->where('lgn_active != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to delete admins
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteAdmin($prdtArray) {
        $loginModel = new Model_Login();
        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_admin) {
                $adminData = Array();
                $adminData = $this->fetchEntryById($_admin);

                if (count($adminData) > 0) {
                    //update status in login table 
                    $login_delete = $loginModel->deleteloginid($adminData['lgn_id']);
                }
            }
        } else {
            $adm_id = $prdtArray;
            $adminData = $this->fetchEntryById($adm_id);
            if (count($adminData) > 0) {
                //update status in login table 
                $login_delete = $loginModel->deleteloginid($adminData['lgn_id']);
            }
        }
    }

}
